package com.lingying.caiwudailikehudaun.controller.dianZiShuiWuJuShenBaoJuLu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lingying.caiwudailikehudaun.domain.shenBao.ShenBaoBaen;
import com.lingying.caiwudailikehudaun.dto.tuPianXinXi.TuPianXinXiDTO;
import com.lingying.caiwudailikehudaun.mapper.dianZiShuiWuJuShenBaoJiLu.DianZiShuiWuJuShenBaoJiLuMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/shenBaoGeShuiIndex")
public class DianZiShuiWuJuShenBaoJiLuController {

    @Resource
    HttpServletRequest httpServletRequest;

    @Resource
    DianZiShuiWuJuShenBaoJiLuMapper dianZiShuiWuJuShenBaoJiLuMapper;


    //（1）电子税务局
    /**
     * 电子税务局申报记录
     * 桑满 20210623
     */
    @GetMapping(value = "/selectShenBaoJiLu")
    public Object selectShenBaoJiLu(
            @RequestParam(value = "shenBaoYueFen",  required = false) String shenBaoYueFen,
            @RequestParam(value = "gongSiMingCheng",  required = false) String gongSiMingCheng,
            @RequestParam(value = "shenBaoRenId",  required = false) Integer shenBaoRenId,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit) {
        JSONObject object = new JSONObject();
//        Integer jiGouId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        Integer zuoZhangShenBaoId = (Integer) httpServletRequest.getSession().getAttribute("zuoZhangShenBaoId");
        Integer pages = (page - 1) * limit;
        List<ShenBaoBaen> selectShenBaoJiLuList = null;
        Integer count = null;
        if (shenBaoYueFen == null){
            //页面没进行搜索时,默认回显上月份的数据
            //获取上个月份
            String suoShuYueFen = "";
            //税务局汇总历史记录
            selectShenBaoJiLuList = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJiLu(zuoZhangShenBaoId,suoShuYueFen,gongSiMingCheng,shenBaoRenId,pages,limit);
            //总个数
            count = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJiLuCount(zuoZhangShenBaoId,suoShuYueFen,gongSiMingCheng,shenBaoRenId);
        }else {
            //当页面进行搜索时.按照页面输入的月份进行搜索
            //税务局汇总历史记录
            selectShenBaoJiLuList = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJiLu(zuoZhangShenBaoId,shenBaoYueFen,gongSiMingCheng,shenBaoRenId,pages,limit);
            //总个数
            count = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJiLuCount(zuoZhangShenBaoId,shenBaoYueFen,gongSiMingCheng,shenBaoRenId);
        }
        if (selectShenBaoJiLuList.size() > 0) {
            object.put("code", 0);
            object.put("data", selectShenBaoJiLuList);
            object.put("count", count);
            object.put("msg", "查询成功");
        } else {
            object.put("code", 500);
            object.put("msg", "无数据");
        }
        return object;
    }

    /**
     * 个税申报记录
     * 桑满 20210623
     */
    @GetMapping(value = "/selectGeShuiShenBaoJiLu")
    public Object selectGeShuiShenBaoJiLu(
            @RequestParam(value = "shenBaoYueFenGeShui",  required = false) String shenBaoYueFenGeShui,
            @RequestParam(value = "gongSiMingChengGeShui",  required = false) String gongSiMingChengGeShui,
            @RequestParam(value = "shenBaoRenIdGeShui",  required = false) Integer shenBaoRenIdGeShui,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit) {
        JSONObject object = new JSONObject();
//        Integer jiGouId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        Integer zuoZhangShenBaoId = (Integer) httpServletRequest.getSession().getAttribute("zuoZhangShenBaoId");
        Integer pages = (page - 1) * limit;
        List<ShenBaoBaen> selectGeShuiShenBaoJiLuList = null;
        Integer count = null;
        if (shenBaoYueFenGeShui == null){
            //页面没进行搜索时,默认回显上月份的数据
            //获取上个月份
//            Calendar c = Calendar.getInstance();
//            c.add(Calendar.MONTH, -1);
//            Date day = c.getTime();
            String suoShuYueFen = "";
            //个税总历史记录
            selectGeShuiShenBaoJiLuList = dianZiShuiWuJuShenBaoJiLuMapper.selectGeShuiShenBaoJiLu(zuoZhangShenBaoId,suoShuYueFen,gongSiMingChengGeShui,shenBaoRenIdGeShui,pages,limit);
            //总个数
            count = dianZiShuiWuJuShenBaoJiLuMapper.selectGeShuiShenBaoJiLuCount(zuoZhangShenBaoId,suoShuYueFen,gongSiMingChengGeShui,shenBaoRenIdGeShui);
        }else {
            //当页面进行搜索时.按照页面输入的月份进行搜索
            //个税总历史记录
            selectGeShuiShenBaoJiLuList = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJiLu(zuoZhangShenBaoId,shenBaoYueFenGeShui,gongSiMingChengGeShui,shenBaoRenIdGeShui,pages,limit);
            //总个数
            count = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJiLuCount(zuoZhangShenBaoId,shenBaoYueFenGeShui,gongSiMingChengGeShui,shenBaoRenIdGeShui);
        }
        if (selectGeShuiShenBaoJiLuList.size() > 0) {
            object.put("code", 0);
            object.put("data", selectGeShuiShenBaoJiLuList);
            object.put("count", count);
            object.put("msg", "查询成功");
        } else {
            object.put("code", 500);
            object.put("msg", "无数据");
        }
        return object;
    }

    /**
     * 查询该公司下已经上传的申报成功的截图（电子税务局）
     * 桑满 20210629
     */
    @RequestMapping(value = "/selectShenBaoJieTuShuiJu", method = RequestMethod.GET)
    public Object selectShenBaoJieTuShuiJu(Integer zuoZhangShenBaoId,String suoShuYueFen) {
        JSONObject object = new JSONObject();
        System.out.println("zuoZhangShenBaoId:" + zuoZhangShenBaoId);

        Map<String, Object> selectShenBaoJieTuShuiJuList = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJieTuShuiJu(zuoZhangShenBaoId, suoShuYueFen);
        System.err.println("查询到的图片：" + selectShenBaoJieTuShuiJuList);

        //方法一：通过得到key的值，然后获取value;
        if (selectShenBaoJieTuShuiJuList.get("dianZiShuiWuJuShenBaoJieTu") == null) {
            object.put("code", 500);
            object.put("msg", "暂无图片");
            return object;
        } else {
            Object valueDD = selectShenBaoJieTuShuiJuList.get("dianZiShuiWuJuShenBaoJieTu");
            List<TuPianXinXiDTO> list = new ArrayList<>();

            JSONObject maps = JSON.parseObject((String) valueDD);
            for (Map.Entry<String, Object> map : maps.entrySet()) {
                TuPianXinXiDTO tuPianXinXiDTO = new TuPianXinXiDTO();

                JSONObject obj = JSON.parseObject(String.valueOf(map.getValue()), JSONObject.class);

                tuPianXinXiDTO.setFileName(obj.getString("fileName"));
                tuPianXinXiDTO.setFilePath(obj.getString("filePath"));
                tuPianXinXiDTO.setRandomName(obj.getString("randomName"));
                System.out.println("map：" + map);
                System.out.println("tuPianXinXiDTO：" + tuPianXinXiDTO);
                list.add(tuPianXinXiDTO);
            }

            System.out.println("list集合：" + list);
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("data", list);
        }
        return object;
    }


    /**
     * 查询该公司下已经上传的申报成功的截图（个税）
     * 桑满 20210629
     */
    @RequestMapping(value = "/selectShenBaoJieTuGeShui", method = RequestMethod.GET)
    public Object selectShenBaoJieTuGeShui(Integer zuoZhangShenBaoId,String suoShuYueFen) {
        JSONObject object = new JSONObject();
        System.out.println("zuoZhangShenBaoId:" + zuoZhangShenBaoId);

        Map<String, Object> selectShenBaoJieTuGeShuiList = dianZiShuiWuJuShenBaoJiLuMapper.selectShenBaoJieTuGeShui(zuoZhangShenBaoId, suoShuYueFen);
        System.err.println("查询到的图片：" + selectShenBaoJieTuGeShuiList);

        //方法一：通过得到key的值，然后获取value;
        if (selectShenBaoJieTuGeShuiList.get("geShuiShenBaoJieTu") == null) {
            object.put("code", 500);
            object.put("msg", "暂无图片");
            return object;
        } else {
            Object valueDD = selectShenBaoJieTuGeShuiList.get("geShuiShenBaoJieTu");
            System.out.println(valueDD);
            List<TuPianXinXiDTO> list = new ArrayList<>();
            JSONObject maps = JSON.parseObject((String) valueDD);
            for (Map.Entry<String, Object> map : maps.entrySet()) {
                TuPianXinXiDTO tuPianXinXiDTO = new TuPianXinXiDTO();
//                    System.out.println(((Map.Entry) map).getKey() + "     " + ((Map.Entry) map).getValue());
//                    System.out.println("集合：" + ((Map.Entry) map).getValue());
                JSONObject obj = JSON.parseObject(String.valueOf(map.getValue()), JSONObject.class);
                tuPianXinXiDTO.setFileName(obj.getString("fileName"));
                tuPianXinXiDTO.setFilePath(obj.getString("filePath"));
                tuPianXinXiDTO.setRandomName(obj.getString("randomName"));
                System.out.println("map：" + map);
                System.out.println("tuPianXinXiDTO：" + tuPianXinXiDTO);
                list.add(tuPianXinXiDTO);
            }

            System.out.println("list集合：" + list);
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("data", list);
            return object;
        }
    }
}
